package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.event.SceneEvent;
	import engine.flag.ResourcesPath;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;
	import newx.com.control.comboBox.NxComboBoxSelectEvent;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;

	public class SendCimeliaLogic
	{
		public static var _self:SendCimeliaLogic;
		private var _ui:SendCimeliaUI;
		private var allList:Array = [];
		private var msgArr:Array = [];
		private var model:ModelLocator;
		private var showCount:int = 10;
		public function SendCimeliaLogic()
		{
		}
		
		public static function getInstance():SendCimeliaLogic
		{
			if(_self == null)
			{
				_self =new SendCimeliaLogic();
			}
			return _self;
		}
		
		
		public function uiinit():void
		{
			model			=	ModelLocator.getInstance();
			allList = StaticIni.getIniVar("getItemList","allList").split(",");
			model			.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_DONATE_CIMILIA ),onSceendSendHandle);
			
			model.addEventListener( SceneEvent.SceneLoaded, onSceneLoaded );
		}
		
		private function onSceneLoaded( evt : SceneEvent ):void
		{
			model.removeEventListener( SceneEvent.SceneLoaded, onSceneLoaded );
			
			updataUI();
		}
		
		
		private function onSceendSendHandle(e:CustomMsgEvent):void
		{
			updataCount();
//			DigCimeliaLogic.getInstance().updataCount();
			var date :Date = new Date(e.pList[0]*1000);
			var msg:String = (StaticIni.getSwfLangStrVar("UIString_1000167",
				[(date.hours<10?"0"+date.hours:date.hours)+":"+
				 (date.minutes<10?"0"+date.minutes:date.minutes)+":"+
				 (date.seconds<10?"0"+date.seconds:date.seconds),
				StaticIni.getSwfLangStr(e.pList[2]),e.pList[1],e.pList[3]]) +"</br>");
			msgArr.push(msg);
			updatatxt();
		}
		
		private function updatatxt():void
		{
			ui.txt_sendinfo.htmlText = "";
			for(var i:int = msgArr.length -1;i>=0 ; i--)
			{
				ui.txt_sendinfo.htmlText += msgArr[i];
			}
		}
		
		
		private function updataUI():void
		{
			var i:int = 0;
			var  itemList_0len:int = ui.pic_list.row*ui.pic_list.col;
			var clip:NxClip ;
			var item:MovieClip;
			var obj:Object ;
			for( i = 0;i< itemList_0len ;i++ )
			{
				obj =TipsMainLogic.instance.getStaticObjWithID(allList[i]);
				item = ui.pic_list.getChildAt(i) as MovieClip;
				clip = new NxClip();
				clip.source = "res\\items\\icon_"+obj.Photo+".jpg";
				clip.addEventListener(MouseEvent.MOUSE_OVER,onListOverHandle);
				clip.addEventListener(MouseEvent.MOUSE_OUT,onListOutHandle);
//				item.addEventListener(MouseEvent.CLICK,onitemClickHandle);
				item["configid"] = allList[i]; 
				item.img2.addChild(clip);
				var lab:NxLabel = new NxLabel();
				lab.visible = false;
				
				
//				var nameLab:NxLabel = new NxLabel();
//				nameLab.text = StaticIni.getSwfLangStr("Description_"+allList[i]);
//				nameLab.x = 50;nameLab.y = 20;
				item.titleTxt2.htmlText = StaticIni.getSwfLangStr(allList[i]);
//				var countlab:NxLabel = new NxLabel();
//				countlab.text = StaticIni.getSwfLangStrVar("UIString_1000170",[StaticIni.getIniVar("Cimelia_"+allList[i],"count")]);
//				countlab.x = 50; countlab.y = 50;
//				item.infoTxt4.width = 100;
//				item.infoTxt4.htmlText = StaticIni.getSwfLangStrVar("UIString_1000170",[StaticIni.getIniVar("Cimelia_"+allList[i],"count")]);
				var sendBtn:NxButton = new NxButton();
				sendBtn.skin = "Button_2";
				sendBtn.label = StaticIni.getSwfLangStr("UIStr_1001366");
				sendBtn.x = 0;sendBtn.y = 70;
				sendBtn.width = 50;
				sendBtn.addEventListener(MouseEvent.CLICK,onitemClickHandle);
				item.addChild(lab);
//				item.addChild(nameLab);
//				item.addChild(countlab);
				item.addChild(sendBtn);
				item["labcount"] = lab;
				
			}
		}
		
		private function onitemClickHandle(e:MouseEvent):void
		{
			var name:String = ui.list.editTitle;
			var mc:MovieClip = e.currentTarget.parent as MovieClip
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_DONATE_CIMILIA,new WString(name),int(mc["configid"]));
			
		}
		
		private function updataCount():void
		{
			var arr:Array = model.scene.GetRecordEx("Cimilia").recArr;
			for(var i:int = 0;i<arr.length;i++)
			{

				var  itemList_0len:int = ui.pic_list.row*ui.pic_list.col;
				var clip:NxClip ;
				var item:MovieClip;
				var obj:Object ;
				for( var j:int = 0;j< itemList_0len ;j++ )
				{
					item = ui.pic_list.getChildAt(j) as MovieClip;
					if(item["configid"] == arr[i][0])
					{
						if(arr[i][1]>0)
						{
							item["labcount"].visible = true;
							item["labcount"].text = arr[i][1];
						}else
						{
							item["labcount"].visible = false;
						}
					}
				}
				
			}
		}
		
		private function updataList():void
		{
			var friendList:Array = model.scene.GetRecordEx("Friend").recArr;
			ui.list.clear();
			for(var i:int = 0;i<friendList.length;i++)
			{
//				var vo:FriendVo = FriendLogic.getInstance().getOneUser(friendList[i],1);
				var obj:Object = {};
				obj.name = friendList[i][0];
				obj.level = friendList[i][5];
				obj.pro = friendList[i][8];
				obj.modelid = friendList[i][10];
				ui.list.addItem({label:obj.name,data:obj});
			}
//			ui.list.dispatchEvent(new NxComboBoxSelectEvent(NxComboBoxSelectEvent.NXCOMBOBOXSELECT_CLICK,ui.list.getItem(0).data));
//			if(ui.list.getItem(0) == null)return;
//			var e:NxComboBoxSelectEvent = new NxComboBoxSelectEvent(NxComboBoxSelectEvent.NXCOMBOBOXSELECT_CLICK,ui.list.getItem(0).data);
//			e.item.data = ui.list.getItem(0).data;
			if(showCount>friendList.length)
			{
				ui.list.showCount = friendList.length;
			}else
			{
				ui.list.showCount = showCount;
			}
//			
//			onListClickHandle(e);
		}
		
		private function onClickCloseHandle(e:NxWindowEvent):void
		{
			ui.hide();
		}
		
		
		private function onListClickHandle(e:NxComboBoxSelectEvent):void
		{
			var obj:Object = e.item.data;
			ui.lab_name.text = obj.name;
//			ui.lab_level.text = "Lv"+obj.level;
			
			var prof:int = obj.pro;
			var profStr:String = "";
			if(prof<10)
			{
				profStr = "00"+prof;
			}else if(prof<100)
			{
				profStr = "0" +prof; 
			}else
			{
				profStr = prof.toString();
			}
//			ui.lab_fro.text = StaticIni.getSwfLangStr("NationString_"+ profStr);
			ui.friendHead.source = ResourcesPath.getHeadPic(obj.modelid);
		}
		
		/**
		 * 
		 * 
		 */
		private function onListOverHandle(e:MouseEvent):void
		{
			var item :NxClip = e.currentTarget as NxClip;
			item.parent.parent
			var obj:Object =TipsMainLogic.instance.getStaticObjWithID(item.parent.parent["configid"]);
			//			var obj:Object = StaticIni.getIniObj(item.configid);//TipsMainLogic.instance.getStaticObjWithID(item.configid);
			if(obj == null)
				return;
			TipsMainLogic.instance.showMain(obj);
//			TipsMainLogic.instance.showMain( StaticIni.getSwfLangStrVar("UIString_1000170",[StaticIni.getIniVar("Cimelia_"+e.currentTarget["configid"],"count")]));
		}
		
		/**
		 * 
		 * 
		 */
		private function onListOutHandle(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();	
		}
		
		/**
		 *添加监听 
		 * 
		 */
		private function addListenter():void
		{
//			ui.pic_list.addItemsEvent(MouseEvent.MOUSE_OVER,onListOverHandle);
//			ui.pic_list.addItemsEvent(MouseEvent.MOUSE_OUT,onListOutHandle);
			ui.list.addEventListener(NxComboBoxSelectEvent.NXCOMBOBOXSELECT_CLICK,onListClickHandle);
			ui.windowBG	.addEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
		}
		
		
		
		/**
		 *移除监听 
		 * 
		 */
		private  function removeListenter():void
		{
//			ui.pic_list.removeItemsEvent(MouseEvent.MOUSE_OVER,onListOverHandle);
//			ui.pic_list.removeItemsEvent(MouseEvent.MOUSE_OUT,onListOutHandle);
			ui.list.removeEventListener(NxComboBoxSelectEvent.NXCOMBOBOXSELECT_CLICK,onListClickHandle);
			ui.windowBG	.removeEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickCloseHandle);
		}
		
		
		public function BeforeShow():void
		{
			updataList();
			updataCount();
			addListenter();
			ui.list.editTitle = "";
		}
		
		public function AfterHide():void
		{
			removeListenter();
			ui.lab_name.text = "";
			ui.friendHead.source = null;
		}
		
		public function get ui():SendCimeliaUI
		{
			if(_ui==null)
			{
				_ui	=	new SendCimeliaUI();
			}
			return _ui
		}
		
	}
}